package com.soccertgc.comm
{
	import com.soccertgc.utils.ClassUtils;

	public class CallErrorType
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		public static const GENERIC:CallErrorType					= new CallErrorType(0, "GENERIC");
		public static const REQUEST:CallErrorType					= new CallErrorType(1, "REQUEST");
		public static const SERVER_EXCEPTION:CallErrorType			= new CallErrorType(2, "SERVER_EXCEPTION");
		
		public static const CALL_FAILED:CallErrorType				= new CallErrorType(9, "CALL_FAILED");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<CallErrorType>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<CallErrorType>();
			_list.push(CallErrorType.GENERIC);
			_list.push(CallErrorType.REQUEST);
			_list.push(CallErrorType.SERVER_EXCEPTION);
			_list.push(CallErrorType.CALL_FAILED);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:int;
		private var _name:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function CallErrorType(id:int, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("CallErrorType enum already created!");
			// Init
			_id    = id;
			_name  = name;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------		
		
		public function get id():int
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names for all possible the values of CallErrorType enumeration
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * Returns all possible the values for the CallErrorType enumeration
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a CallErrorType by given id 
		 * @param id Id of the card type to find
		 * @return PlayerCallErrorType constant for the given id, null if no CallErrorType
		 * for the given id has been found
		 */		
		public static function getById(id:int):CallErrorType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("CallErrorType for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a CallErrorType by given name 
		 * @param name Name of the player CallErrorType to find
		 * @return PlayerCallErrorType constant for the given id, null if no CallErrorType
		 * for the given name has been found
		 */		
		public static function getByName(name:String):CallErrorType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("CallErrorType for name '" + name + "' unknown!");
		}
		
	}
}